<?php
/**
 * Created by PhpStorm.
 * User: Korbin
 * Date: 2018/10/30
 * Time: 15:12
 */

/**
 * @param $url
 * @param array $data
 * @param string $httpHeader
 * @return mixed
 */
function curl_get_https($url, $data = [], $httpHeader = '')
{
    if (!empty($data)) {
        foreach ($data as $key => $val) {
            $url .= '&' . $key . '=' . $val;
        }
    }
    //初始化
    $ch = curl_init();
    //将https 变为http方式post
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    if (!empty ($httpHeader) && is_array($httpHeader)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    //打印获得的数据
    return $output;
}

/**
 * CURL请求POST方式
 *
 * @param string $url
 * @param string|array $data
 * @param string $httpHeader
 * @return Json
 */
function curlPost($url, $data, $httpHeader = '')
{
    $post_data = is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
    $ch = curl_init(); // 初始化curl


    //将https 变为http方式post
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    // 参数设置
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    if (!empty ($httpHeader) && is_array($httpHeader)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    }
    $result = curl_exec($ch);
    if ($result === false)         // 连接失败
    {
        print curl_error($ch);
    }
    curl_close($ch);
    return $result;
}

/**
 * CURL请求POST x-www-form-urlencoded方式
 *
 * @param string $url
 * @param string|array $data
 * @param string $httpHeader
 * @return Json
 */
function curl_post_formurlen($url, $data, $httpHeader = '')
{
    //$post_data = is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
    $post_data = http_build_query($data);
    $ch = curl_init(); // 初始化curl


    //将https 变为http方式post
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    // 参数设置
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    if (!empty ($httpHeader) && is_array($httpHeader)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    }
    $result = curl_exec($ch);
    if ($result === false)         // 连接失败
    {
        print curl_error($ch);
    }
    curl_close($ch);
    return $result;
}

/** 生成UUID
 * @return string
 */
if (!function_exists('com_create_guid')){
    function com_create_guid()
    {

        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

            mt_rand(0, 0xffff), mt_rand(0, 0xffff),

            mt_rand(0, 0xffff),

            mt_rand(0, 0x0fff) | 0x4000,

            mt_rand(0, 0x3fff) | 0x8000,

            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)

        );

    }
}


/**
 * 验证视频尺寸
 *
 * @param $width
 * @param $height
 * @return int
 */
function getVideoSizeType($width, $height)
{
    $type = 2;//正方形
    if (intval($width) > intval($height)) {
        $type = 0;//宽屏
    } elseif (intval($width) < intval($height)) {
        $type = 1;//竖屏
    }
    return $type;
}

/**
 * 检测中文
 * @param $string
 * @return int
 */
function checkChinese($string)
{
    if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $string) === 1) {
        //全是中文
        return 1;
    } elseif (preg_match('/[\x{4e00}-\x{9fa5}]/u', $string) === 1) {
        //包含中文
        return 0;
    }
    return -1;
}